using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;

public partial class Admin_SanPham_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
        }
    }

    private void LoadDropDownList()
    {
        tbDanhMucSanPhamCollection dmCollection = tbDanhMucSanPhamBUS.GetAll();
        ddl_dm.DataTextField = tbDanhMucSanPhamColumns.tendanhmuc.ToString();
        ddl_dm.DataValueField = tbDanhMucSanPhamColumns.madanhmuc.ToString();
        ddl_dm.DataSource = dmCollection;
        ddl_dm.DataBind();

       

        tbDoanhNghiepCollection dnCollection = tbDoanhNghiepBUS.GetAll();
        ddl_dn.DataTextField = tbDoanhNghiepColumns.tendn.ToString();
        ddl_dn.DataValueField = tbDoanhNghiepColumns.madn.ToString();
        ddl_dn.DataSource = dnCollection;
        ddl_dn.DataBind();
    }

    protected void btn_Them_Click(object sender, EventArgs e)
    {
        try
        {
            tbSanPham sanpham = new tbSanPham();
            
            sanpham.tensp = txt_TenSP.Text;
            sanpham.tinvantat = txt_tinvantat.Text;
            sanpham.danhmuc = int.Parse(ddl_dm.SelectedValue);
            sanpham.doanhnghiep  = int.Parse(ddl_dn .SelectedValue);
            sanpham.giagoc = float.Parse(txt_giagoc.Text);
            sanpham.giaban = float.Parse(txt_giaban.Text);
            sanpham.soluongban = float.Parse(txt_soluongban.Text);
            sanpham.soluongtoida = float.Parse(txt_soluongtoida.Text);
            sanpham .damua = 0;
            sanpham.chietkhau = float .Parse (txt_chietkhau .Text );
            sanpham.ngaysudung = DateTime .Parse (txt_ngaysudung.Text);
            sanpham.ngayhethan =DateTime .Parse(txt_ngayhethan.Text);
            sanpham.tinhtrang = 12;  //12 la ma cua SP dang cho
            sanpham.mota  = ftb_mota .Text;
            sanpham.diemnoibat = ftb_diemnoibat.Text;
            sanpham.dieukien = ftb_dieukien.Text;
            sanpham.quydinh = ftb_quydinh.Text;
            sanpham.thongtinchitiet = ftb_thongtinchitiet.Text;
            if (fu_HinhMinhHoa.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_HinhMinhHoa.FileName;
                fu_HinhMinhHoa.SaveAs(Server.MapPath("~/Photos/" + filename));
                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));
                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
                sanpham.hinhchinh = filename;
            }
            else
            {
                sanpham.hinhchinh  = "no_images.jpg";
            }
            tbSanPhamBUS.Insert(sanpham);
         }
        catch
        {
            
        }
        Response.Redirect("~/Admin/SanPham_List.aspx");
    }
}
